﻿using System;
using System.Web;
using Joel.Net;
using umbraco.BusinessLogic;

namespace UComment.Spam
{
    public class AkismetSpamChecker: SpamChecker   {       

        public AkismetSpamChecker()
        {
            ProviderName = "Akismet";
        }

        public override Boolean Check(int nodeid,
            string userAgent, string userIp, string author,
            string authorEmail, string authorUrl, string content)
        {
            var api = Initialize(nodeid);

            if (api != null)
            {
                var comment = new AkismetComment
                                {
                                    UserAgent = userAgent,
                                    UserIp = userIp,
                                    CommentType = "comment",
                                    CommentAuthor = author,
                                    CommentAuthorEmail = authorEmail,
                                    CommentAuthorUrl = authorUrl,
                                    CommentContent = content
                                };

                 return api.CommentCheck(comment);
            }
            return false;
        }

        public override void MarkAsHam(int nodeid, string author, string authorEmail, string authorUrl, string content)
        {
            var api = Initialize(nodeid);
            if (api == null) return;

            var comment = new AkismetComment
                              {
                                  CommentType = "comment",
                                  CommentAuthor = author,
                                  CommentAuthorEmail = authorEmail,
                                  CommentAuthorUrl = authorUrl,
                                  CommentContent = content
                              };

            api.SubmitHam(comment);
        }

        public override void MarkAsSpam(int nodeid, string author, string authorEmail, string authorUrl, string content)
        {
            var api = Initialize(nodeid);

            if (api == null) return;

            var comment = new AkismetComment
                              {
                                  CommentType = "comment",
                                  CommentAuthor = author,
                                  CommentAuthorEmail = authorEmail,
                                  CommentAuthorUrl = authorUrl,
                                  CommentContent = content
                              };

            api.SubmitSpam(comment);
        }


        private Akismet Initialize(int nodeid)
        {
            var apikey = Config.GetSpamCheckerSetting("akismetAPIKey");

            if (apikey == string.Empty)
            {
                Log.Add(LogTypes.Error, -1, "Akismet Key could not be found, please add a valid Akismet API Key to UComment.config");
                return null;
            }

            var api = new Akismet(
                    apikey,
                    "http://" + HttpContext.Current.Request.ServerVariables["HTTP_HOST"],
                    "UComment");

            if (!api.VerifyKey())
            {
                Log.Add(LogTypes.Error, -1, "Akismet Key could not be verified, please check if you have a valid Akismet API Key");
                return null;
            }
            
            return api;
            

            
        }
    }
}
